/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package Mainframe;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import GuiTools.*;
import Settings.*;
import Storage.*;
import Translate.Translate;

public class LookUpPanel extends JPanel{
	Storage MAINVOC;
	Language LANG;
	public TribuitTextField searchTF;
    JComboBox vocCB;
    JPanel bottomPanel,optPanel,btnPanel,searchPanel;
    LabelPanel lp;
    TribuitFrame MAINFRAME;
    ExtendButton extendBtn,goBtn;
    LookUpOptions LUOptions;
    boolean open=false;
    
    public void focus(){
    	searchTF.grabFocus();
    }
    
	public LookUpPanel(Settings set, Storage str,Mainframe tf){
		super();
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		bottomPanel=this;
		MAINFRAME=tf;
		MAINVOC=str;
		LANG=set.getLanguage();
		LUOptions = new LookUpOptions(set,tf);
		Color c = set.getBackgroundColor();
		float[] f = Color.RGBtoHSB(c.getRed(), c.getBlue(), c.getGreen(),null);
		if(f[2]>0.9)c= Color.black;
		else c = Color.white;
		searchTF = new TribuitTextField(set,10);
    	searchTF.addActionListener(new SearchListener());
    	searchTF.setFocusable(true);
    	searchTF.addKeyListener(new KeyAdapter(){
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_UP || e.getKeyCode()==KeyEvent.VK_DOWN) LUOptions.showUp(LANG);
			}
    	});
    	String[] names = MAINVOC.getNamesWithWords();
    	extendBtn = new ExtendButton("opts");
    	goBtn = new ExtendButton(">>>");
    	goBtn.setToolTipText(LANG.getLookup());
    	goBtn.addActionListener(new SearchListener());
    	if(names==null){
    		names = new String[0];
    		vocCB = new JComboBox(names);
    		searchTF.setText(LANG.getNoWords());
    		searchTF.setEnabled(false);
    		goBtn.setEnabled(false);
    		extendBtn.setEnabled(false);
    		vocCB.setEnabled(false);
    	}
    	else vocCB = new JComboBox(names);
    	if(MAINVOC.getVocabulariesWithWords()!=null){
    		vocCB.addItem(LANG.getAll());
    		vocCB.setSelectedItem(LANG.getAll());
    	}
    	vocCB.setPreferredSize(new Dimension(100,vocCB.getPreferredSize().height));
    	JLabel inLabel = new JLabel("in");
    	inLabel.setForeground(c);
    	btnPanel = new JPanel();
    	btnPanel.setLayout(new BoxLayout(btnPanel,BoxLayout.Y_AXIS));
    	btnPanel.add(goBtn);
    	btnPanel.add(extendBtn);
    	btnPanel.setBackground(set.getBackgroundColor());
    	
    	searchPanel = new JPanel();
    	searchPanel.add(searchTF);
    	searchPanel.add(inLabel);
    	searchPanel.add(vocCB);
    	searchPanel.add(btnPanel);
    	searchPanel.setBackground(set.getBackgroundColor());
    	lp = new LabelPanel(" "+LANG.getLookup()+":",searchPanel);
    	lp.label.setForeground(c);
    	lp.setBackground(set.getBackgroundColor());
    	lp.setBorder(null);
    	this.add(lp);
    	this.setBorder(null);
    	extendBtn.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			LUOptions.showUp(LANG);
    		}
    	});
    	extendBtn.setToolTipText(LANG.getOptions());
    	
	}
	
	public void updateColor(Color color){
		btnPanel.setBackground(color);
		searchPanel.setBackground(color);
		lp.setBackground(color);
	}
	
	
	public class SearchListener implements ActionListener{
    	public void actionPerformed(ActionEvent e){
			String[] opts = {"ok"};
			String txt = searchTF.getText();
			
			if(!txt.equals("")){
				Translate translate = new Translate(txt, LUOptions.getSearchMethod(),(String)vocCB.getSelectedItem());
				translate.setCrawlMetas(LUOptions.getCrawlMetas());
				translate.setConsCase(LUOptions.getConsiderCase());
				translate.setIncludePreArg(LUOptions.getConsiderPreFix());
				translate.setLanguage(LANG);
				translate.setStorage(MAINVOC);
			}
			else{ 
				JOptionPane.showOptionDialog(MAINFRAME,LANG.getInputEmpty(),LANG.getAttention(),JOptionPane.YES_OPTION,JOptionPane.WARNING_MESSAGE,null,opts,null);
				searchTF.grabFocus();
			}
		}
    }
}